package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.CourseService;
import com.lanchetech.bean.request.PageReq;
import com.lanchetech.bean.response.BasePageResp;
import com.lanchetech.bean.response.BaseResp;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.bean.vo.*;
import com.lanchetech.entity.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "课程管理接口", tags = "课程管理接口")
@RequestMapping("/api/course")
@RestController
public class CourseController {

    @Autowired
    CourseService courseService;

    @OperatorLog("编辑专栏")
    @ApiOperation("编辑专栏")
    @PreAuthorize("hasAuthority('course.column-index.edit.auth')")
    @PostMapping("/editColumns")
    public BaseResp editColumns(@RequestBody Columns req) {
        return courseService.editColumns(req);
    }

    @ApiOperation("获取专栏详情")
    @GetMapping("/getColumnsDetail")
    public ResultData<Columns> getColumnsDetail(@RequestParam("id") Long id) {
        return courseService.getColumnsDetail(id);
    }

    @OperatorLog("删除专栏")
    @ApiOperation("删除专栏")
    @PreAuthorize("hasAuthority('course.column-index.delete.auth')")
    @GetMapping("/deleteColumns")
    public BaseResp deleteColumns(@RequestParam("id") Long id) {
        return courseService.deleteColumns(id);
    }

    @ApiOperation(value = "获取专栏分页", notes = "筛选条件 title, name->teacherName, type, status")
    @PreAuthorize("hasAuthority('course.column-index')")
    @PostMapping("/getColumnsPage")
    public ResultData<BasePageResp<Columns>> getColumnsPage(@RequestBody PageReq req) {
        return courseService.getColumnsPage(req);
    }

    @ApiOperation("获取所有专栏列表")
    @GetMapping("/getColumnsList")
    public ResultData<List<Columns>> getColumnsList() {
        return courseService.getColumnsList();
    }

    @ApiOperation("获取所有课程列表（根据专栏ids）")
    @PostMapping("/getCourseListByColumnIds")
    public ResultData<List<Course>> getCourseListByColumnIds(@RequestBody List<Long> columnIds) {
        return courseService.getCourseListByColumnIds(columnIds);
    }

    @OperatorLog("编辑课程")
    @ApiOperation("编辑课程")
    @PreAuthorize("hasAuthority('course.course-index.edit.auth')")
    @PostMapping("/editCourse")
    public BaseResp editCourse(@RequestBody Course req) {
        return courseService.editCourse(req);
    }

    @ApiOperation("获取课程详情")
    @GetMapping("/getCourseDetail")
    public ResultData<Course> getCourseDetail(@RequestParam("id") Long id) {
        return courseService.getCourseDetail(id);
    }

    @OperatorLog("删除课程")
    @ApiOperation("删除课程")
    @PreAuthorize("hasAuthority('course.course-index.delete.auth')")
    @GetMapping("/deleteCourse")
    public BaseResp deleteCourse(@RequestParam("id") Long id) {
        return courseService.deleteCourse(id);
    }

    @ApiOperation(value = "获取课程分页", notes = "筛选条件 title, type->auth, status, columnId")
    @PreAuthorize("hasAuthority('course.course-index')")
    @PostMapping("/getCoursePage")
    public ResultData<BasePageResp<Course>> getCoursePage(@RequestBody PageReq req) {
        return courseService.getCoursePage(req);
    }

    @OperatorLog("编辑SKU关联的专栏或课程")
    @ApiOperation("编辑SKU关联的专栏或课程")
    @PreAuthorize("hasAuthority('product.spu.course.auth')")
    @PostMapping("/editSkuColumnCourse")
    public BaseResp editSkuColumnCourse(@RequestBody List<SkuColumnCourse> req) {
        return courseService.editSkuColumnCourse(req);
    }

    @OperatorLog("删除SKU关联的所有专栏或所有课程")
    @ApiOperation("删除SKU关联的所有专栏或所有课程")
    @PreAuthorize("hasAuthority('product.spu.course.auth')")
    @GetMapping("/deleteSkuColumnCourse")
    public BaseResp deleteSkuColumnCourse(@RequestParam("skuId") Long skuId, @RequestParam("type") Byte type) {
        return courseService.deleteSkuColumnCourse(skuId, type);
    }

    @ApiOperation("获取SKU列表分页，显示已绑定的专栏或课程数量")
    @PreAuthorize("hasAuthority('product.course-setting')")
    @PostMapping("/getSkuPageWithBindCount")
    public ResultData<BasePageResp<SkuCourseBindVO>> getSkuPageWithBindCount(@RequestBody PageReq req) {
        return courseService.getSkuPageWithBindCount(req);
    }

    @ApiOperation(value = "获取SKU关联的专栏或课程详情", notes = "返回的是一个数组")
    @GetMapping("/getSkuColumnCourseDetail")
    public ResultData<List<SkuColumnCourse>> getSkuColumnCourseDetail(@RequestParam("skuId") Long skuId, @RequestParam("type") Byte type) {
        return courseService.getSkuColumnCourseDetail(skuId, type);
    }

    @ApiOperation(value = "获取课程授权分页", notes = "筛选条件 userId,spuId,skuId,columnId,courseId,type,relateId->orderSkuId,startTime,endTime")
    @PreAuthorize("hasAuthority('course.course-auth')")
    @PostMapping("/getColumnCourseAuthPage")
    public ResultData<BasePageResp<ColumnCourseAuthVO>> getColumnCourseAuthPage(@RequestBody PageReq req) {
        return courseService.getColumnCourseAuthPage(req);
    }

    @ApiOperation("获取作业分页")
    @PreAuthorize("hasAuthority('course.homework')")
    @PostMapping("/getHomeworkPage")
    public ResultData<BasePageResp<HomeworkVO>> getHomeworkPage(@RequestBody PageReq req) {
        return courseService.getHomeworkPage(req);
    }

    @ApiOperation("获取学分分页")
    @PostMapping("/getExpFlowPage")
    public ResultData<BasePageResp<ExpFlowVO>> getExpFlowPage(@RequestBody PageReq req) {
        return courseService.getExpFlowPage(req);
    }

    @OperatorLog("编辑作业回复")
    @ApiOperation("编辑作业回复")
    @PreAuthorize("hasAuthority('course.homework.view.auth')")
    @PostMapping("/editHomeworkReply")
    public BaseResp editHomeworkReply(@RequestBody Homework req) {
        return courseService.editHomeworkReply(req);
    }

}
